package example.hello_security.login.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import example.hello_security.login.bean.MyUser;
import example.hello_security.login.constant.UserConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    PasswordEncoder passwordEncoder;

    public static void main( String[] args ) {
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder( );
        System.out.println( bCryptPasswordEncoder.encode( "admin" ) );
    }


    @GetMapping("/register")
    public JSONPObject register(String username,String password) {
        String encodedPassword = passwordEncoder.encode(password);
        MyUser user = new MyUser(username, encodedPassword);
        UserConstant.userMap.put(username,user);
        System.out.println(encodedPassword );
        JSONPObject jsonpObject=new JSONPObject("code","200" );
        return jsonpObject;
    }
}
